package com.masterclient.app.modules.shop.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.shop.DealerProductFilterItemBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.adapter.DealerProductManagerFilterAdapter;

import java.util.List;

/**
 * @auther hjian
 * @date 2020/11/5.
 */
public class DealerProductManagerFilterPopup extends PopupWindow {

    private DealerProductManagerFilterAdapter filterAdapter;
    private SelectCallback callBack;
    private List<DealerProductFilterItemBean> tabs;
    private int selectIndex = 0;


    public interface SelectCallback{
        void onSelect(int position);
    }

    public DealerProductManagerFilterPopup(Context context, List<DealerProductFilterItemBean> tabs, int selectIndex, SelectCallback callBack) {
        super(context);
        this.callBack = callBack;
        this.tabs = tabs;
        this.selectIndex = selectIndex;
        initView(context, tabs);
    }

    public void setTabs(List<DealerProductFilterItemBean> tabs, int selectIndex ) {
        this.tabs = tabs;
        this.selectIndex = selectIndex;
        if (filterAdapter != null) {
            filterAdapter.setNewData(tabs);
            filterAdapter.setSelectIndex(selectIndex);
        }
    }

    private void initView(Context context, List<DealerProductFilterItemBean> tabs) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = mInflater.inflate(R.layout.pop_material_sequence, null);

        RecyclerView recyclerView = (RecyclerView) mView.findViewById(R.id.pop_material_sequence_recycler);
        recyclerView.setPadding(ScreenUtils.dp2px(12),ScreenUtils.dp2px(12),ScreenUtils.dp2px(12),ScreenUtils.dp2px(10));
        recyclerView.setLayoutManager(new GridLayoutManager(context,3));
        filterAdapter = new DealerProductManagerFilterAdapter();
        recyclerView.setAdapter(filterAdapter);
        filterAdapter.setNewData(tabs);

        filterAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (selectIndex == position) return;
                selectIndex = position;
                filterAdapter.setSelectIndex(position);
                callBack.onSelect(position);
            }
        });
        mView.findViewById(R.id.pop_material_sequence_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        this.setContentView(mView);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setTouchable(true);


        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getWindowVisibleDisplayFrame(rect);
            Activity activity = (Activity) anchor.getContext();
            Rect outRect1 = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
            int h = outRect1.height() - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }
}
